package com.module.msg.view

import android.content.Context
import androidx.fragment.app.Fragment
import com.module.app.base.BaseFragment
import com.module.base.utils.DisplayUtils
import com.module.base.utils.StatusMyBarUtil
import com.module.base.utils.ViewPager2Helper
import com.module.base.widget.pager.MyFragmentAdapter
import com.module.msg.R
import com.module.msg.databinding.MsgFragMsgBinding
import com.module.msg.model.MsgViewModel
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ClipPagerTitleView
import skin.support.content.res.SkinCompatResources

class MsgFragment : BaseFragment<MsgViewModel, MsgFragMsgBinding>() {

    private val commonNavigator: CommonNavigator by lazy {
        CommonNavigator(context)
    }
    private val defaultPosition = 0
    private val fragmentList: MutableList<Fragment> =
        mutableListOf(HotMsgFragment(), MyMsgFragment())
    private val titles: MutableList<String> = mutableListOf("热门投稿", "我的投稿")

    override fun bingViewModel() {
        mDatabind.vm = mViewModel
    }

    override fun onLazyLoad() {
    }

    override fun initView() {
        super.initView()
        StatusMyBarUtil.setPaddingSmart(context, mDatabind.layoutHead)
        initViewPage()
    }


    private fun initViewPage() {
        val adapter = MyFragmentAdapter(this, fragmentList)
        mDatabind.viewPager.adapter = adapter
        mDatabind.viewPager.offscreenPageLimit = fragmentList.size
        commonNavigator.isAdjustMode = false
        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            override fun getCount(): Int {
                return fragmentList?.size ?: 0
            }

            override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                val clipPagerTitleView = ClipPagerTitleView(getContext())
                clipPagerTitleView.text = titles[index]
                clipPagerTitleView.textSize = DisplayUtils.dip2px(20f).toFloat()
                clipPagerTitleView.textColor =
                    SkinCompatResources.getColor(getContext(), R.color.bg_tab_top_content2)
                clipPagerTitleView.clipColor =
                    SkinCompatResources.getColor(getContext(), R.color.bg_tab_top_content)
                clipPagerTitleView.setOnClickListener { mDatabind.viewPager.currentItem = index }
                return clipPagerTitleView
            }

            override fun getIndicator(context: Context): IPagerIndicator? {
                return null
            }
        }
        mDatabind.indicator.navigator = commonNavigator
        ViewPager2Helper.bind(mDatabind.indicator, mDatabind.viewPager)
        mDatabind.viewPager.setCurrentItem(defaultPosition, false)
    }

}